<template>
  <div>
    <!-- data: 表格对应的数据 -->
    <!-- row-key：选择的依据，通过id选择 -->
    <!-- default-expand-all：默认展开所有子集 -->
    <!-- tree-props：属性数据的属性 -->
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
    >
      <el-table-column prop="firstcatename" label="一级分类" width="100">
      </el-table-column>
      <el-table-column prop="secondcatename" label="二级分类" width="100">
      </el-table-column>
      <el-table-column prop="goodsname" label="商品名称" width="100">
      </el-table-column>
      <el-table-column label="图片">
        <template slot-scope="scope">
          <img v-if="scope.row.img" :src="$host + scope.row.img" width="100" />
        </template>
      </el-table-column>
      <el-table-column prop="market_price" label="市场价格" width="100">
      </el-table-column>
      <el-table-column prop="price" label="销售价格" width="100">
      </el-table-column>
      <el-table-column label="是否新品">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isnew"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
            @change="itemChange(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="是否热卖">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.ishot"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
            @change="itemChange(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="success">正常</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            circle
            @click="edit(scope.row)"
          ></el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            circle
            @click="del(scope.row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页：1.总条数（后端知道，前端不知道，需要发请求）2.每页显示几条（前端知道）3.分成几页=总条数/每页显示几条，如果有余数就向上取整 4.当前页码数 -->
    <!-- current-change：修改页码，就是跳转页面 -->
    <!-- current-page：页码数 -->
    <!-- page-size：每页显示几条 -->
    <!-- layout：要显示的控件 -->
    <!-- total：总条数 -->
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="size"
      layout="total, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getGoodsCount, goodsList, userDel } from "../../api";
import list from "../category/list.vue";
export default {
  components: { list },
  data() {
    return {
      tableData: [],
      // 当前页码数
      currentPage: 1,
      // 每页显示几条
      size: 5,
      // 总条数
      total: 1,
    };
  },
  created() {
    this.getTableList();
  },
  methods: {
    // 修改页码数，获取另一页的数据
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.getList();
    },
    // 修改是否新品
    itemChange(val) {
      console.log(val);
    },
    async getTableList() {
      let res = await getGoodsCount();
      this.total = res.list[0].total;
      // 获取列表
      this.getList();
    },
    // 获取列表
    async getList() {
      let res = await goodsList({ page: this.currentPage, size: this.size });
      this.tableData = res.list;
    },
    // 编辑
    edit(info) {
      // 先子传父，后父传子  list->index->info
      this.$emit("edit", info);
    },
    // 删除
    del(uid) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        // 真的删除
        .then(async () => {
          let res = await userDel(uid);
          if (res.code === 200) {
            // 后台返回的数据没有分页，所以重新获取第一页的数据
            this.currentPage = 1; // 返回第一页
            this.getTableList();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          } else {
            this.$message({
              type: "error",
              message: res.msg,
            });
          }
        })
        // 取消删除
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style>
</style>